package com.ibatis.service.impl;

import com.ibatis.common.resources.Resources;
import com.ibatis.pojo.Student;
import com.ibatis.service.StudentService;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StudentServiceImpl
 * @Description: TODO
 * @Author: Sunny
 * @Create: 2023-11-03 09:35
 * @Version 1.0
 **/
public class StudentServiceImpl implements StudentService {
    static SqlMapClient sqlMapClient = null;
    static {
        String resource = "SqlMapConfig.xml";

        try {
            Reader reader = Resources.getResourceAsReader(resource);

            sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);

        } catch (Exception e) {
            e.printStackTrace();

        }

    }
    @Override
    public void addStudent(Student student) {
        try {
            sqlMapClient.insert("insertStudent", student);

        } catch (SQLException e) {
            e.printStackTrace();

        }
    }
    @Override
    public int deleteStudent(Student student) {
        int effectedRow = 0;

        try {
            effectedRow = sqlMapClient.delete("deleteStudent", student);

        } catch (SQLException e) {
            e.printStackTrace();

        }
        return effectedRow;

    }

    @SuppressWarnings("unchecked")

    @Override
    public List<Student> getAllStudents() {
        List<Student> students = null;
        try {
            students = sqlMapClient.queryForList("getAllStudents");

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;
    }

    @Override

    public Student getStudentById(int id) {
        Student student = null;
        try {
            student = (Student) sqlMapClient.queryForObject("getStudentById", id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return student;
    }

    @Override
    public int updateStudent(Student student) {
        int effectedRow = 0;
        try {
            effectedRow = sqlMapClient.update("updateStudent", student);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return effectedRow;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Student> getStudentsLike(Map<String,String> map) {
        List<Student> students = null;
        try {
            students = sqlMapClient.queryForList("getStudentsLike", map);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;

    }
    @SuppressWarnings("unchecked")
    @Override
    public List<Student> getStudentsEquel(Map<String,String> map) {
        List<Student> students = null;
        try {
            students = sqlMapClient.queryForList("getStudentsEquel", map);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Student> getStudentsTest(Map<String,String> map) {
        List<Student> students = null;
        try {
            students = sqlMapClient.queryForList("getStudentsTest", map);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return students;
    }
}